# -*- coding: utf-8 -*-

import os
import pickle
import glob
import numpy as np
import pandas as pd
import pmdarima as pm
import threading



import sys
import json
import datetime

# 通过身份证号获取年龄
def get_age(cid):
    now = datetime.datetime.now()
    birth_year = int(cid[6:10])
    birth_month = int(cid[10:12])
    birth_day = int(cid[12:14])
    age = now.year - birth_year - ((now.month, now.day) < (birth_month, birth_day))
    return age

# 通过身份证号获取性别
def get_sex(cid):
    sex = int(cid[16:17])
    return '男' if sex % 2 == 1 else '女'

def calculate_user_score(user_data):
    base_score = 599

    # 从输入数据解析用户信息
    cid = user_data["idCard"]
    work_type = user_data["workType"]
    education = user_data["education"]
    marital_status = user_data["maritalStatus"]
    annual_income = user_data["annualIncome"]

    # 计算年龄和性别
    age = get_age(cid)
    sex = get_sex(cid)

    # 初始化分数
    score = base_score

    # 根据年龄调整分数
    if age <= 31:
        score -= 19
    elif 31 < age <= 49:
        score -= 10
    else:
        score += 68

    # 根据工作类型调整分数
    if work_type == '公务员':
        score += 40
    elif work_type in ['自由职业', '其他']:
        score += 5
    elif work_type == '私营个体':
        score -= 36
    elif work_type == '退休人员':
        score+=26

    # 根据学历调整分数
    if education in ['文盲', '小学']:
        score -= 47
    elif education in ['初中', '高中']:
        score += 4
    elif education == '大学及以上':
        score += 79

    # 根据婚姻状况调整分数
    if marital_status == '已婚':
        score += 27
    elif marital_status == '未婚':
        score -= 32
    else:
        score-=87

    # 根据年收入调整分数
    if annual_income <= 43776.0:
        score -= 23
    elif 43776.0 < annual_income <= 75744.0:
        score += 17
    else:
        score += 17

    return score

if __name__ == "__main__":
    # 从标准输入读取JSON数据
    input_json = sys.stdin.read()

    try:
        # 解析JSON为Python字典
        user_data = json.loads(input_json)

        # 计算信用分
        credit_score = calculate_user_score(user_data)

        # 创建结果JSON
        result = {
            "userId": user_data["userId"],
            "creditScore": credit_score
        }

        # 输出结果
        print(json.dumps(result))

    except Exception as e:
        # 错误处理
        error_result = {"error": str(e)}
        print(json.dumps(error_result))
        sys.exit(1)